package com.zhaosc.elasticsearch;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.junit.Test;

import com.zhaosc.utils.SleepUtils;

/**
 * https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.5/java-rest-high-document-exists.html
 * 
 * @author zhaosc
 *
 */
public class ExistsTest extends BaseTest {

	private static final Logger logger = Logger.getLogger(BaseTest.class);

	@Test
	public void testExists01() {

		GetRequest getRequest = new GetRequest(index, "1");
		getRequest.fetchSourceContext(new FetchSourceContext(false));
		getRequest.storedFields("_none_");
		try {
			boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
			logger.info("***" + exists);
		} catch (IOException e) {

			logger.error(e);
		}
	}

	@Test
	public void testExists02() {

		GetRequest getRequest = new GetRequest(index, "1");
		getRequest.fetchSourceContext(new FetchSourceContext(false));
		getRequest.storedFields("_none_");

		ActionListener<Boolean> listener = new ActionListener<Boolean>() {
			@Override
			public void onResponse(Boolean exists) {
				logger.info("**result**" + exists);
			}

			@Override
			public void onFailure(Exception e) {
				logger.error(e);
			}
		};
		client.existsAsync(getRequest, RequestOptions.DEFAULT, listener);

		SleepUtils.sleep(1000L);
	}

}
